

var car=new Vue({
	el: '#confirmorder',
	data: {
		carorders:{
			totalPrice:"",
			userid:0,
			infos:[]
		},
		setting:{},
		days:[],
		ordertime:""
	},
	methods:{
		confirmClick:function(){
			//点击确认预定
			if(car.ordertime==""){
				mui.toast('请先选择预定时间!');
				return;
			}
			mui.showLoading("正在加载中...","div");
			
			var data1={
				userid:car.carorders.userid,
				ordertotalprice:car.carorders.totalPrice,
				ordertime:car.ordertime,
				ordertype:11
			};
			var goods=[];
			for(var i in car.carorders.infos){
				var info=car.carorders.infos[i];
				goods.push({
					goodid:info.bunsid,
					goodname:info.bunsname,
					goodnum:info.chancenum,
                    goodunitprice:info.bunsunitprice
				})
			}
			
			data1["orderGoods"]=goods
			console.log(JSON.stringify(data1));
			$.ajax({
				type:"post",
				url:sthost+"/addOrder",
				data:JSON.stringify(data1),
				contentType: "application/json; charset=utf-8",  
        			dataType: "json",  
				success:function(obj){
					mui.hideLoading(function(){
						if(obj.result==0){
						mui.toast("预定成功");
						mui.back();
					}else{
						mui.toast("预定失败");
					}
					});
					
					
					
				},
				error:function(){
					mui.hideLoading(function(){
						mui.toast("预定失败");
					});
				}
			});
		}
	}
});

mui.init();
mui.previewImage();
mui.ready(function(){
	//alert(sessionStorage.getItem("cars"));
	var cars=JSON.parse(sessionStorage.getItem("cars"));
	car.carorders.totalPrice=cars.totalPrice;
	car.carorders.userid=cars.userid;
	for(var i in cars.infos){
//		info["unittotalprice"]=accMul(info.bunsunitprice,info.chancenum);
		var info=cars.infos[i];
			car.carorders.infos.push(info);
	}
getBunSetting();
	document.querySelector('#ul-order-time').addEventListener('selected',function(e){
//			var d=e.t.index();
//			var index=$('.mui-table-view .mui-table-view-radio').index(d);
			car.ordertime=e.target.title;
			console.log(e.target.title);
		});
});


function getBunSetting(){
	$.ajax({
		type:"get",
		url:sthost+"getAllGoodSetting",
		success:function(obj){
			console.log(JSON.stringify(obj));
			for(var i in obj.data){
				var sett=obj.data[i];
				car.setting[sett.keyName]=sett.keyValue;
			}
			dayGetSetting();
		}
	});
}



function dayGetSetting(){
	var week=getWeeks(Date.parse(new Date()));//本周周数组
	var cuttentWeekDay=getWeeksday(Date.parse(new Date()));//当前周几
	console.log(week);
	var dayArray=car.setting.BUNS_ORDER_WEEK.split(",");//设置周几参数数组
	var isNextWeek=false;
	if(dayArray.length!=0){
		if(dayArray[dayArray.length-1]<cuttentWeekDay){
			week=getNextWeek();
			//下周参数数组
			for(var i in dayArray){
				var d=dayArray[i];
		  		car.days.push({
				name:"下周"+d,
				weektime:week[d-1]
			})
		  	
			
		}}else{
            for(var i in dayArray){
                var d=dayArray[i];
		  	if(d>cuttentWeekDay){
		  		car.days.push({
				name:"本周"+d,
				weektime:week[d-1].fomattertime
				
			})
		  	}
			}
		}
	}
	
	console.log(car.days);
	console.log(getNextWeek());
	
}


function getWeeks(target) {
        var now = new Date(target);
        var now_day= now.getDay();
        var now_time = now.getTime();
        var result = [1,2,3,4,5,6,7];
        var e=[];
        for(var i in result){
        	e.push(	{
                day:new Date(now_time + 24*60*60*1000*(i - now_day)).getDate(),
                fomattertime:new Date(now_time + 24*60*60*1000*(i - now_day)).format("yyyy-MM-dd")
            });
		}
        return e;
    }
function getWeeksday(target) {
        var now = new Date(target);
        var now_day= now.getDay();
        var now_time = now.getTime();
        var result = [1,2,3,4,5,6,7]
        return result[now_day-1];  
    }


function 	getNextWeek() {
    const week = [];
    for (var i = 0; i < 7; i++) {
      var Stamp = new Date();
      var number;
      if (Stamp.getDay() == 0) {
        number = 7;
      } else {
        number = Stamp.getDay();
      }
      var num = 7-number + 1 + i;
      Stamp.setDate(Stamp.getDate() + num);

      var year = Stamp.getFullYear();
      var month = Stamp.getMonth() + 1;
      var date = Stamp.getDate();
      if (month < 10) {
          month = '0'+month;//这里使用的反引号
      }
      if (date < 10) {
        week.push(year+'-'+month+'-0'+date);//这里使用的反引号
      } else {
        week.push(year+'-'+month+'-'+date);//这里使用的反引号
      }
    }
    return week;
  }


Date.prototype.format = function(fmt){  
    var o = {  
         "M+": this.getMonth()+1,  
         "d+": this.getDate(),  
         "H+": this.getHours(),  
         "m+": this.getMinutes(),  
         "s+": this.getSeconds(),  
         "S+": this.getMilliseconds()  
    };  
  
    //因位date.getFullYear()出来的结果是number类型的,所以为了让结果变成字符串型，下面有两种方法：  
  
      
  
    if(/(y+)/.test(fmt)){  
        //第一种：利用字符串连接符“+”给date.getFullYear()+""，加一个空字符串便可以将number类型转换成字符串。  
  
        fmt=fmt.replace(RegExp.$1,(this.getFullYear()+"").substr(4-RegExp.$1.length));  
    }  
    for(var k in o){  
        if (new RegExp("(" + k +")").test(fmt)){  
  
            //第二种：使用String()类型进行强制数据类型转换String(date.getFullYear())，这种更容易理解。  
  
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(String(o[k]).length)));  
        }  
    }     
    return fmt;  
}  
